#!/bin/bash

echo "Defining Parameters..."
cat data/parameters.dat | grep xsize > temp
sed -i "s/\txsize//g" temp
xsize=$(sed "s/\#//g" temp)
echo "...xsize=$xsize"
cat data/parameters.dat | grep ysize > temp
sed -i "s/\tysize//g" temp
ysize=$(sed "s/\#//g" temp)
echo "...ysize=$ysize"
cat data/parameters.dat | grep steps > temp
sed -i "s/\tsteps//g" temp
cellsize=$(sed "s/\#//g" temp)
cat data/parameters.dat | grep minhight > temp
sed -i "s/\tminhight//g" temp
minhight=$(sed "s/\#//g" temp)
echo "...minhight=$minhight"
cat data/parameters.dat | grep maxhight > temp
sed -i "s/\tmaxhight//g" temp
maxhight=$(sed "s/\#//g" temp)
echo "...maxhight=$maxhight"
cat data/parameters.dat | grep minshadow > temp
sed -i "s/\tminshadow//g" temp
minshadow=$(sed "s/\#//g" temp)
echo "...minshadow=$minshadow"
cat data/parameters.dat | grep maxshadow > temp
sed -i "s/\tmaxshadow//g" temp
maxshadow=$(sed "s/\#//g" temp)
echo "...maxshadow=$maxshadow"

rm temp

mkdir data/Slab_Hight
mkdir data/Shadow_Zone
mkdir data/Pdep
mkdir data/Per
mkdir data/mov

echo "Plotting height for time steps..."
for i in data/raw/hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png size 1400,600
   	set output "data/Slab_Hight/pic-$(basename $i .dat).png"
   	unset key
	set grid
  	set size ratio -1


   	set cbrange [$minhight:$maxhight]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" u 1:2:3 with image
EOF
done

mencoder mf://data/Slab_Hight/pic-hight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/movhight.avi >/dev/null


echo "Plotting Shadow Zone for time steps..."
for i in data/hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png size 1400,600
   	set output "data/Shadow_Zone/shadow$(basename $i .dat).png"
   	unset key
	set grid
  	set size ratio -1

   	set cbrange [$minshadow:$maxshadow]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" u 1:2:7 with image
EOF
done

mencoder mf://data/Shadow_Zone/shadowhight*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/zone.avi >/dev/null


echo "Plotting erosion probability for time steps..."
for i in data/hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png size 1400,600
   	set output "data/Per/per$(basename $i .dat).png"
   	unset key
	set grid
  	set size ratio -1

   	set cbrange [0:1]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" u 1:2:6 with image
EOF
done

mencoder mf://data/Per/per*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/Perodion.avi >/dev/null


echo "Plotting deposition probability for time steps..."
for i in data/hight*.dat; do

#echo "$(basename $i .dat)"
   	gnuplot << EOF
   	set terminal png size 1400,600
   	set output "data/Pdep/pdep$(basename $i .dat).png"
   	unset key
	set grid
  	set size ratio -1

   	set cbrange [0:1]
   	set xrange [0:$xsize]
   	set yrange [0:$ysize]
   	plot "$i" u 1:2:5 with image
EOF
done

mencoder mf://data/Pdep/pdep*.png -mf fps=5:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -o data/mov/Pdeposition.avi >/dev/null


mkdir data/raw
#mv data/hight* data/raw/
